<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\ProductCategories;
use App\Http\Controllers\Controller;
use App\Products;

class CategoryController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // parent::__construct();
    }

    public function index(Request $request, $cate_id = '', $order = '')
    {
        $categories = ProductCategories::where('pid', 0)
                                    ->paginate(15);

        if($cate_id) {
            $products = Products::where('category_id', $cate_id);
        } else {
            $products = Products::where('category_id', $cate_id = 1);
        }

        if($order == 'date') {
            $products->orderBy('created_at', 'Desc');
        } 
        else if($order == 'price') {
            $products->orderBy('price', 'asc');
        }
        $products = $products->get()->toArray();

        return view('home.category_index', [
            'categories' => $categories,
            'limitedProducts' => $products,
            'cate_id' => $cate_id
        ]);
    }

    
}
